import router from 'umi/router';
import { message } from 'antd';
import { getCurrentIpRank,getAllDevices,getAllIpRank,getFreshIp } from './services';

export default {
  namespace: 'ipRank',
  state: {
    allDevices:[]
  },

  effects: {

    *getAllDevices({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getAllDevices, payload);
        if(res.data.length>0) {
          callback(res.data[0]);
        }
        yield put({
          type: 'save',
          payload: {
            allDevices: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      }
    },

    *getCurrentIpRank({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getCurrentIpRank, payload);
        callback(res.data)
      } catch (e) {
        message.error(e.message);
      }
    },

    *getAllIpRank({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getAllIpRank, payload);
        callback(res.data)
      } catch (e) {
        message.error(e.message);
      }
    },

    *getFreshIp({ payload}, { call, put }) {
      try {
        yield call(getFreshIp, payload);
        message.info("刷新成功");
      } catch (e) {
        message.error(e.message);
      }
    },
  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
